package com.xiaoyu.base.data;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xiaoyu.base.event.member.MemberUpdateEvent;
import com.xiaoyu.base.event.superrecommend.SuperRecommendUpdateEvent;
import com.xiaoyu.base.event.wallet.CPCoinUpdateEvent;
import com.xiaoyu.base.utils.time.CountDown;

import in.srain.cube.request.JsonData;

public class ExtUserStatusData {

    private static final ExtUserStatusData ourInstance = new ExtUserStatusData();

    private int vipRemainDays = 0;
    private int sVipRemainDays = 0;
    private int cpCoinRemainNumber = 0;
    private int superRecommendRemainTickets = 0;
    private boolean limitSuperRecommend;
    private LimitChatInfo limitChatInfo;

    private ExtUserStatusData() {
    }

    public static ExtUserStatusData getInstance() {
        return ourInstance;
    }

    public void updateAndSave(JsonData jsonData) {
        if (TextUtils.isEmpty(jsonData.toString()) || jsonData.length() <= 0) {
            return;
        }
        updateMemberRemainDay(jsonData);
        updateCPCoinRemainNumber(jsonData);
        updateSuperRecommendRemainTickets(jsonData);
        updateLimitSuperRecommend(jsonData);
        updateLimitChatInfo(jsonData);
        save();
    }

    private boolean isVip() {
        return vipRemainDays > 0;
    }

    public boolean isSVip() {
        return sVipRemainDays > 0;
    }

    public boolean isMember() {
        return isVip() || isSVip();
    }

    public int getCPCoinRemainNumber() {
        return cpCoinRemainNumber;
    }

    public boolean isLimitSuperRecommend() {
        return limitSuperRecommend;
    }

    public int getSuperRecommendRemainTickets() {
        return superRecommendRemainTickets;
    }

    public LimitChatInfo getLimitChatInfo() {
        return limitChatInfo;
    }

    public void updateSuperRecommendRemainTickets(int count) {
        superRecommendRemainTickets = count;
    }

    private void updateSuperRecommendRemainTickets(final JsonData jsonData) {
        int tempSuperRecommendRemainTickets = jsonData.has("super_recommend_remain_tickets") ? jsonData.optInt("super_recommend_remain_tickets") : superRecommendRemainTickets;
        if (tempSuperRecommendRemainTickets != superRecommendRemainTickets) {
            superRecommendRemainTickets = tempSuperRecommendRemainTickets;
            new SuperRecommendUpdateEvent().post();
        }
    }

    private void updateCPCoinRemainNumber(final JsonData jsonData) {
        int tempCPCoinRemainNumber = jsonData.has("cp_coin_remain_number") ? jsonData.optInt("cp_coin_remain_number") : cpCoinRemainNumber;
        if (tempCPCoinRemainNumber != cpCoinRemainNumber) {
            cpCoinRemainNumber = tempCPCoinRemainNumber;
            new CPCoinUpdateEvent().post();
        }
    }

    private void updateMemberRemainDay(final JsonData jsonData) {
        int tempVipRemainDays = jsonData.has("vip_remain_days") ? jsonData.optInt("vip_remain_days") : vipRemainDays;
        int tempSVipRemainDays = jsonData.has("svip_remain_days") ? jsonData.optInt("svip_remain_days") : sVipRemainDays;

        if (tempVipRemainDays != vipRemainDays || tempSVipRemainDays != sVipRemainDays) {
            vipRemainDays = tempVipRemainDays;
            sVipRemainDays = tempSVipRemainDays;
            new MemberUpdateEvent().post();
        }
    }

    private void updateLimitSuperRecommend(final JsonData jsonData) {
        boolean tempLimitSuperRecommend = jsonData.optBoolean("super_recommend_limit");
        if (tempLimitSuperRecommend != limitSuperRecommend) {
            limitSuperRecommend = tempLimitSuperRecommend;
        }
    }

    private void updateLimitChatInfo(JsonData jsonData) {
        LimitChatInfo newLimitChatInfo = new LimitChatInfo(jsonData.optJson("limit_chat_info"));
        if (limitChatInfo == null || !limitChatInfo.equals(newLimitChatInfo)) {
            limitChatInfo = newLimitChatInfo;
        }
    }

    private void save() {
        UserData.getInstance().saveUserKV(UserKVConfigData.KEY_EXT_USER_STATUS, toJson());
    }

    private JsonData toJson() {
        JsonData jsonData = JsonData.newMap();
        jsonData.put("vip_remain_days", vipRemainDays);
        jsonData.put("svip_remain_days", sVipRemainDays);
        jsonData.put("cp_coin_remain_number", cpCoinRemainNumber);
        jsonData.put("super_recommend_remain_tickets", superRecommendRemainTickets);
        jsonData.put("super_recommend_limit", limitSuperRecommend);
        jsonData.put("limit_chat_info", limitChatInfo.toString());
        return jsonData;
    }

    public static class LimitChatInfo {
        final JsonData jsonData;

        public final String toast;
        public final CountDown countdown;

        LimitChatInfo(JsonData jsonData) {
            this.jsonData = jsonData;
            this.toast = jsonData.optString("toast");
            this.countdown = CountDown.createFromJson(jsonData.optJson("count_down"));
        }

        @Override
        public boolean equals(@Nullable Object o) {
            if (this == o) {
                return true;
            }
            if (!(o instanceof LimitChatInfo)) {
                return false;
            }
            LimitChatInfo other = (LimitChatInfo) o;
            if (!TextUtils.equals(toast, other.toast)) {
                return false;
            }
            return countdown.equals(other.countdown);
        }

        @NonNull
        @Override
        public String toString() {
            return jsonData.toString();
        }
    }
}
